//
// Copyright 2023 The GUAC Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package backend

// // getPreloads get the specific graphQL query fields that are requested.
// // graphql.CollectAllFields only provides the top level fields and none of the nested fields below it.
// // getPreloads recursively goes through the fields and retrieves each nested field below it.
// // for example:
// /*
//   type
//   namespaces {
//     namespace
//     names {
//       name
//       tag
//       commit
//     }
//   }
//   will return:
//   fields: [type namespaces namespaces.namespace namespaces.names namespaces.names.name namespaces.names.tag namespaces.names.commit]
// */
// func getPreloads(ctx context.Context) ([]string, bool) {
// 	if !graphql.HasOperationContext(ctx) {
// 		return []string{}, false
// 	}

// 	visited := make(map[string]bool)
// 	return getNestedPreloads(
// 		graphql.GetOperationContext(ctx),
// 		graphql.CollectFieldsCtx(ctx, nil),
// 		"", visited), true
// }

// func getNestedPreloads(ctx *graphql.OperationContext, fields []graphql.CollectedField, prefix string, visited map[string]bool) []string {
// 	preloads := []string{}
// 	for _, column := range fields {
// 		prefixColumn := getPreloadString(prefix, column.Name)
// 		if visited[prefixColumn] {
// 			continue
// 		}
// 		visited[prefixColumn] = true
// 		preloads = append(preloads, prefixColumn)
// 		preloads = append(preloads, getNestedPreloads(ctx, graphql.CollectFields(ctx, column.Selections, nil), prefixColumn, visited)...)
// 	}
// 	return preloads
// }

// func getPreloadString(prefix, name string) string {
// 	if len(prefix) > 0 {
// 		return prefix + "." + name
// 	}
// 	return name
// }
